using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Emlyn.Applications.Scratch.Graphics.Tools
{
    public class MapData: List<int>
    {
        public MapData()
        {
            _width = -1;
            _height = -1;
        }

        private int _height;

	    public int Height
	    {
		    get { return _height;}
		    set { _height = value;}
	    }

        private int _width;

	    public int Width
	    {
		    get { return _width;}
		    set { _width = value;}
	    }

        public void LoadFromFile(string aFileName)
        {
            Clear();

            using (StreamReader lReader = File.OpenText(aFileName))
            {
                int lwidth = -1;
                int lheight = 0;

                while (!lReader.EndOfStream)
                {
                    string lline = lReader.ReadLine();

                    string[] ldata = lline.Split(',');

                    foreach (string ldatum in ldata)
                    {
                        if (ldatum.Trim().Length > 0)
                        {
                            if (lwidth == -1)
                                lwidth = ldata.Length-1;

                            this.Add(Convert.ToInt32(ldatum));
                        }
                    }

                    lheight++;
                }

                Width = lwidth;
                Height = lheight;
            }
        }

        public void LoadFromFile(string aFileName, int aWidth)
        {
            Clear();

            using (StreamReader lReader = File.OpenText(aFileName))
            {
                int lwidth = -1;
                int lheight = 0;

                while (!lReader.EndOfStream)
                {
                    string lline = lReader.ReadLine();

                    string[] ldata = lline.Split(',');

                    foreach (string ldatum in ldata)
                    {
                        if (ldatum.Trim().Length > 0)
                        {
                            this.Add(Convert.ToInt32(ldatum));
                        }
                    }

                    lheight++;
                }

                Width = aWidth;
                Height = Count / aWidth;
            }
        }

        public int GetData(int x, int y)
        {
            int retval = -1;

            if (x >= 0 && x < Width && y >= 0 && y < Height)
            {
                retval = this[x + y * Width];
            }

            return retval;    
        }

        public void QuadrupleSize()
        {
            int lCount = this.Count;
            List<int> ltemplist = new List<int>();

            for (int y = 0; y < Height; y++)
            {
                for (int x = 0; x < Width; x++)
                {
                    ltemplist.Add(this[x + y * Width]);
                }
                for (int x = 0; x < Width; x++)
                {
                    ltemplist.Add(this[x + y * Width]);
                }
            }
            for (int y = 0; y < Height; y++)
            {
                for (int x = 0; x < Width; x++)
                {
                    ltemplist.Add(this[x + y * Width]);
                }
                for (int x = 0; x < Width; x++)
                {
                    ltemplist.Add(this[x + y * Width]);
                }
            }

            this.Clear();
            foreach (int ldatum in ltemplist)
            {
                this.Add(ldatum);
            }

            Height = Height * 2;
            Width = Width * 2;
        }
    }
}
